classdef point
    properties
        xloc=rand*20-10;
        yloc=rand*20-10;
        zloc=0;
        dummy=-10:.25:10;
        xvel=randn(1,1);
        yvel=randn(1,1);
        trav=0;
        h;
    end
    methods
        function obj=point(Dummy)
            obj.dummy=Dummy;
            obj.xloc=rand()*20-10;
            obj.yloc=rand()*20-10;
        end
        function obj=plop(obj,Z) 
            %% Place the dot randomly on the grid
            obj.xloc=rand()*20-10;
            obj.yloc=rand()*20-10;
            obj.zloc=interp2(obj.dummy,obj.dummy,Z,obj.xloc,obj.yloc,'linear');
            if(isempty(obj.h))
                obj.h=plot3(obj.xloc,obj.yloc,obj.zloc,'.');
                set(obj.h,'MarkerSize',20);
            else
                set(obj.h,'xdata',obj.xloc,'ydata',obj.yloc,'zdata',interp2(obj.dummy,obj.dummy,Z,obj.xloc,obj.yloc,'linear'));   
            end
            obj.trav=0;
        end
        function obj=move(obj,x,y,Z)
            %% 
            % Calculate Accel
            xaccl = x - obj.xloc;
            yaccl = y - obj.yloc;
            
            norm = sqrt(xaccl^2 + yaccl^2);
            
            if (norm > 1)
                xaccl = xaccl / norm;
                yaccl = yaccl / norm;
            end
            %%
            % Calculate new velocity
            obj.xvel=obj.xvel + xaccl;
            obj.yvel=obj.yvel + yaccl;
            
            %%
            % Friction
            obj.yvel=obj.yvel * 0.95;
            obj.xvel=obj.xvel * 0.95;
            
            
            %%
            % Move by velocity
            obj.xloc=obj.xloc + obj.xvel/10;
            obj.yloc=obj.yloc + obj.yvel/10;
            
            %%
            % Find new height
            obj.zloc=interp2(obj.dummy,obj.dummy,Z,obj.xloc,obj.yloc,'linear');
            
            %%
            % Replot
            set(obj.h,'xdata',obj.xloc,'ydata',obj.yloc,'zdata',obj.zloc);
        end
    end
end